package com.bocai.yuweiwangluo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.View.AutoScrollViewPager;
import com.bocai.yuweiwangluo.View.LoopViewPager;
import com.bocai.yuweiwangluo.View.XListView;
import com.bocai.yuweiwangluo.activity.FootballTalkActivity;
import com.bocai.yuweiwangluo.activity.PublishTalkActivity;
import com.bocai.yuweiwangluo.activity.TalkDetailActivity;
import com.bocai.yuweiwangluo.adapter.TalkListAdapter;
import com.bocai.yuweiwangluo.base.BaseFragment;
import com.bocai.yuweiwangluo.bean.BBSHomeBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.MyConst;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;


/**
 * Created by admin on 2015/9/16.
 */
public class Fragment_Talk_new extends BaseFragment implements View.OnClickListener,AdapterView.OnItemClickListener,XListView.IXListViewListener{
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private XListView mlv_talk_list;
    private View view;
    private AutoScrollViewPager vp_home;
    private List<View> dotsVenue; // 场馆的那些点
    // 定义的三个指示点
    private View dot3;
    private View dot4;
    private View dot5;
    private LinearLayout ll_title_right,ll_talk_football,ll_talk_basketball,ll_talk_yumaoqiu,ll_talk_wangqiu;
    private TextView title_mame;
    private ImageView iv_title_right,iv_title_left;
    private BBSHomeBean bbsHomeBean;
    private String type_id="";
    private View viewHeader;
    public static final int REQUST_OK=-1;
    private LinearLayout llRefresh;

    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            String result = (String) msg.obj;
            Gson gson = new Gson();
            mlv_talk_list.stopRefresh();
            try
            {
                switch (msg.what)
                {
                    case RESPONSE_SUCCEED:
                        hideLoading();
                        Log.e("result", result);
                        bbsHomeBean= gson.fromJson(result, BBSHomeBean.class);
                        MyConst.bbstype=bbsHomeBean.getType();
                        BBSBannerAdapter bannerAdapter=new BBSBannerAdapter(getFragmentManager());
                        vp_home.setOnPageChangeListener(new PageChangeListener());
                        vp_home.setAdapter(bannerAdapter);
                        vp_home.startAutoScroll();
                        vp_home.setInterval(5000);
                        TalkListAdapter mTalkListAdapter=new TalkListAdapter(getActivity(),bbsHomeBean);
                        mlv_talk_list.setAdapterNoFoot(mTalkListAdapter);
                        break;
                    case RESPONSE_FAILED:
                        hideLoading();
                        llRefresh.setVisibility(View.VISIBLE);
                        showError(result);
                        break;
                }
            } catch (Exception e)
            {
            }
        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_talk, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        getData();
        llRefresh=(LinearLayout)view.findViewById(R.id.ll_refresh);
        viewHeader=LayoutInflater.from(getActivity()).inflate(R.layout.fragment_talk_header,null);
        title_mame=(TextView)view.findViewById(R.id.title_mame);
        title_mame.setText("论坛");
        iv_title_right=(ImageView)view.findViewById(R.id.iv_title_right);
        iv_title_right.setImageResource(R.mipmap.title_right_add);
        iv_title_left=(ImageView)view.findViewById(R.id.iv_title_left);
        iv_title_left.setVisibility(View.GONE);
        ll_title_right=(LinearLayout)view.findViewById(R.id.ll_title_right);
        ll_talk_football=(LinearLayout)viewHeader.findViewById(R.id.ll_talk_football);
        ll_talk_basketball=(LinearLayout)viewHeader.findViewById(R.id.ll_talk_basketball);
        ll_talk_yumaoqiu=(LinearLayout)viewHeader.findViewById(R.id.ll_talk_yumaoqiu);
        ll_talk_wangqiu=(LinearLayout)viewHeader.findViewById(R.id.ll_talk_wangqiu);
        mlv_talk_list=(XListView)view.findViewById(R.id.mlv_talk_list);
        vp_home=(AutoScrollViewPager)viewHeader.findViewById(R.id.vp_home);
        mlv_talk_list.addHeaderView(viewHeader);
        dotsVenue=new ArrayList<View>();
        dot3 = view.findViewById(R.id.v_dot3);
        dot4 = view.findViewById(R.id.v_dot4);
        dot5 = view.findViewById(R.id.v_dot5);
        dotsVenue.add(dot3);
        dotsVenue.add(dot4);
        dotsVenue.add(dot5);
        initEvent();
        // 设置一个监听器，当ViewPager中的页面改变时调用
//        addDynamicView2();
    }

    private void initEvent() {
        vp_home.setEnabled(true);
        llRefresh.setOnClickListener(this);
        ll_title_right.setOnClickListener(this);
        ll_talk_basketball.setOnClickListener(this);
        ll_talk_football.setOnClickListener(this);
        ll_talk_wangqiu.setOnClickListener(this);
        ll_talk_yumaoqiu.setOnClickListener(this);
        mlv_talk_list.setOnItemClickListener(this);
        mlv_talk_list.setFocusable(false);
        mlv_talk_list.setPullLoadEnable(false);
        mlv_talk_list.setPullRefreshEnable(true);
        mlv_talk_list.setXListViewListener(this);
    }

    private void getData()
    {
        showLoading();
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(getActivity(),this);
        myOkHttpClient.getbbsHome(UrlData.BASEURL + UrlData.BBSHOME, handler);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode==2000)
        {
            if(resultCode==REQUST_OK)
            {
                onRefresh();
            }
        }
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ll_refresh:
                llRefresh.setVisibility(View.GONE);
                onRefresh();
                break;
            case R.id.ll_title_right:
                Intent intentpublish=new Intent(getActivity(), PublishTalkActivity.class);
                startActivityForResult(intentpublish,2000);
                break;
            case R.id.ll_talk_football:
                try
                {
                    Intent intent=new Intent(getActivity(), FootballTalkActivity.class);
                    for(int i=0;i<bbsHomeBean.getType().size();i++)
                    {
                        if("足球".equals(bbsHomeBean.getType().get(i).getTitle()))
                        {
                            type_id=bbsHomeBean.getType().get(i).getId();
                        }
                    }
                    intent.putExtra("type_id",type_id);
                    intent.putExtra("type","足球论坛");
                    startActivityForResult(intent,2000);
                }catch (Exception e)
                {

                }

                break;
            case R.id.ll_talk_basketball:
                Intent intent1=new Intent(getActivity(), FootballTalkActivity.class);
                for(int i=0;i<bbsHomeBean.getType().size();i++)
                {
                    if("篮球".equals(bbsHomeBean.getType().get(i).getTitle()))
                    {
                        type_id=bbsHomeBean.getType().get(i).getId();
                    }
                }
                intent1.putExtra("type_id",type_id);
                intent1.putExtra("type","篮球论坛");
                startActivityForResult(intent1,2000);
                break;
            case R.id.ll_talk_yumaoqiu:
                try{
                    Intent intent2=new Intent(getActivity(), FootballTalkActivity.class);
                    for(int i=0;i<bbsHomeBean.getType().size();i++)
                    {
                        if("羽毛球".equals(bbsHomeBean.getType().get(i).getTitle()))
                        {
                            type_id=bbsHomeBean.getType().get(i).getId();
                        }
                    }
                    intent2.putExtra("type_id",type_id);
                    intent2.putExtra("type","羽毛球论坛");
                    startActivityForResult(intent2,2000);
                }catch (Exception e)
                {

                }

                break;
            case R.id.ll_talk_wangqiu:
                try {
                    Intent intent3=new Intent(getActivity(), FootballTalkActivity.class);
                    for(int i=0;i<bbsHomeBean.getType().size();i++)
                    {
                        if("网球".equals(bbsHomeBean.getType().get(i).getTitle()))
                        {
                            type_id=bbsHomeBean.getType().get(i).getId();
                        }
                    }
                    intent3.putExtra("type_id",type_id);
                    intent3.putExtra("type","网球论坛");
                    startActivityForResult(intent3,2000);
                }catch (Exception e)
                {

                }

                break;

        }
    }
    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
    @Override
    public void onResume()
    {
        super.onResume();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        BBSHomeBean.Data data= (BBSHomeBean.Data) parent.getAdapter().getItem(position);
        Intent intent=new Intent(getActivity(), TalkDetailActivity.class);
        intent.putExtra("id",data.getId());
        startActivityForResult(intent, 2000);
    }

    @Override
    public void onRefresh()
    {
        getData();
    }

    @Override
    public void onLoadMore()
    {

    }
    @Override
    public void onStop()
    {
        super.onStop();
    }

    private class PageChangeListener implements LoopViewPager.OnPageChangeListener
    {

        private int oldPositionVenue = 0;

        @Override
        public void onPageScrollStateChanged(int arg0)
        {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2)
        {

        }

        @Override
        public void onPageSelected(int position)
        {
            position = (bbsHomeBean.getBanner().size() + position%bbsHomeBean.getBanner().size())%bbsHomeBean.getBanner().size();
            dotsVenue.get(oldPositionVenue).setBackgroundResource(R.drawable.dot_normal);
            dotsVenue.get(position).setBackgroundResource(R.drawable.dot_focused2);
            oldPositionVenue = position;
        }
    }
    class BBSBannerAdapter extends FragmentPagerAdapter
    {

        public BBSBannerAdapter(FragmentManager fm)
        {
            super(fm);
        }

        @Override
        public Fragment getItem(int position)
        {
            position = (bbsHomeBean.getBanner().size() + position%bbsHomeBean.getBanner().size())%bbsHomeBean.getBanner().size();
            BBSBannerFrag fragment = new BBSBannerFrag();
            fragment.getData(bbsHomeBean.getBanner().get(position));
            return fragment;
        }

        @Override
        public CharSequence getPageTitle(int position)
        {
            return bbsHomeBean.getBanner().get(position % bbsHomeBean.getBanner().size()).getPic().toUpperCase();
        }

        @Override
        public int getCount()
        {
            return bbsHomeBean.getBanner().size();
        }
    }

}
